
import 'package:flustars/flustars.dart';
import 'package:json_annotation/json_annotation.dart';

import '../../../utils/string_utils.dart';
import '../../../widget/w_toast.dart';

part 'leads.g.dart';

@JsonSerializable()
class Leads {
  ///线索ID
  int? id;
  ///客户名称
  @JsonKey(defaultValue: "", toJson: strToJson)
  String? customerName;
  ///客户类型
  int? customerType;
  ///直营店
  int? shopId;
  @JsonKey(defaultValue: "", toJson: strToJson)
  String? shopName;
  ///性别
  int? sex;
  ///年龄
  @JsonKey(defaultValue: "", fromJson: stringFromInt)
  String? age;
  ///手机号
  @JsonKey(defaultValue: "", toJson: strToJson)
  String? phone;
  ///意向级别
  int? level;
  ///意向车型
  @JsonKey(defaultValue: "", toJson: strToJson)
  String? modelId;
  @JsonKey(defaultValue: "")
  String? modelName;
  ///销售顾问
  int? saleman;
  ///销售顾问名称
  @JsonKey(defaultValue: "", toJson: strToJson)
  String? salemanName;
  ///线索来源
  int? clueSource;
  ///线索状态
  int? status;
  ///微信
  @JsonKey(defaultValue: "", toJson: strToJson)
  String? wechat;
  ///公司名称
  @JsonKey(defaultValue: "", toJson: strToJson)
  String? companyName;
  ///活动类型
  int? activityId;
  ///活动类型名称
  @JsonKey(defaultValue: "", toJson: strToJson)
  String? activityName;
  ///证件类型
  int? idType;
  ///证件号码
  @JsonKey(defaultValue: "", toJson: strToJson)
  String? idNo;
  ///省
  int? province;
  @JsonKey(defaultValue: "")
  String? provinceName;
  ///市
  int? city;
  @JsonKey(defaultValue: "")
  String? cityName;
  ///区
  int? county;
  @JsonKey(defaultValue: "")
  String? countyName;
  ///详细地址
  @JsonKey(defaultValue: "", toJson: strToJson)
  String? address;
  ///线索下发时间
  @JsonKey(fromJson: fullDateFromJson, toJson: strToJson)
  String? distributeDate;
  ///意向购车日期
  @JsonKey(fromJson: ymdDateFromJson, toJson: strToJson)
  String? purchaseDate;
  ///是否置换
  int? isSubstitution;
  ///是否贷款
  int? isLoan;
  ///预算金额
  @JsonKey(defaultValue: "")
  String? budgetAmount;
  ///客户生日
  @JsonKey(fromJson: ymdDateFromJson, toJson: strToJson)
  String? birthday;
  ///邮箱
  @JsonKey(defaultValue: "", toJson: strToJson)
  String? eMail;
  ///联系人
  @JsonKey(defaultValue: "", toJson: strToJson)
  String? contactName;
  ///联系人电话
  @JsonKey(defaultValue: "", toJson: strToJson)
  String? contactPhone;
  ///附件
  List<Map<String, dynamic>>? filesList;
  ///客户标签
  List<Map<String, dynamic>>? appCustomerTagList;
  ///线索创建时间
  @JsonKey(fromJson: fullDateFromJson, toJson: strToJson)
  String? createdAt;
  ///备注
  @JsonKey(defaultValue: "", toJson: strToJson)
  String? remark;
  ///下次跟进时间
  @JsonKey(fromJson: ymdDateFromJson, toJson: strToJson)
  String? nextFollowDate;
  ///跟进次数
  int? followTimes;
  ///跟进结果
  int? defeatInvalidResult;
  ///战败或者无效原因
  int? defeatInvalidReason;
  ///战败或者无效申请时间
  @JsonKey(fromJson: fullDateFromJson, toJson: strToJson)
  String? defeatInvalidTime;
  ///是否选中
  bool isSelected;
  ///是否逾期
  bool? overdueFlag;

  Leads({this.id, this.customerName, this.customerType, this.shopId, this.shopName, this.age, this.phone, this.level, this.modelId, this.modelName, this.saleman, this.salemanName, this.clueSource, this. status, this.sex,this.wechat, this.companyName, this.activityId, this.activityName, this.idType, this.idNo, this.province, this.provinceName, this.city, this.cityName, this.county, this.countyName, this.address, this.distributeDate,this.purchaseDate, this.isSubstitution, this.isLoan, this.budgetAmount, this.birthday, this.eMail, this.contactName, this.contactPhone, this.filesList, this.appCustomerTagList,this.createdAt,this.remark, this.nextFollowDate, this.followTimes, this.isSelected = false,this.defeatInvalidResult, this.defeatInvalidReason, this.defeatInvalidTime,this.overdueFlag});

  factory Leads.fromJson(Map<String, dynamic> json) => _$LeadsFromJson(json);

  Map<String,dynamic> toJson() => _$LeadsToJson(this);

  Map<String,dynamic> toVoJson() => {
    "id": id,
    "shopId": shopId,
    "customerName": strToJson(customerName),
    "sex": sex,
    "age": age,
    "customerType": customerType,
    "phone": strToJson(phone),
    "wechat": strToJson(wechat),
    "companyName": strToJson(companyName),
    "saleman": saleman,
    "salemanName": salemanName,
    "clueSource": clueSource,
    "activityId": activityId,
    "idType": idType,
    "idNo": idNo,
    "province": province,
    "city": city,
    "county": county,
    "address": address,
    "level": level,
    "modelId": modelId,
    "purchaseDate": strToJson(purchaseDate),
    "isSubstitution": isSubstitution,
    "isLoan": isLoan,
    "budgetAmount": budgetAmount,
    "birthday": strToJson(birthday),
    "eMail": eMail,
    "contactName": strToJson(contactName),
    "contactPhone": strToJson(contactPhone),
    "appCustomerTagList": appCustomerTagList,
    "filesList": filesList,
    "remark": strToJson(remark),
  };

  static bool check(Leads leads) {
    if (StringUtils.isEmpty2(leads.customerName)) {
      Toast.toast("请输入客户名称");
      return false;
    }
    if (StringUtils.isEmpty2(leads.sex)) {
      Toast.toast("请选择客户性别");
      return false;
    }
    if (StringUtils.isEmpty2(leads.phone)) {
      Toast.toast("请输入客户手机号");
      return false;
    }
    if (!StringUtils.isChinaPhoneLegal(leads.phone!)) {
      Toast.toast("请输入正确的手机号");
      return false;
    }
    if (StringUtils.isEmpty2(leads.customerType)) {
      Toast.toast("请选择客户类型");
      return false;
    }
    if (leads.idType == 24311001 && !StringUtils.isCardId(leads.idNo!)) {
      Toast.toast("请输入正确的身份证号");
      return false;
    }
    if (!StringUtils.isEmpty2(leads.idNo) && !StringUtils.isCardNumber(leads.idNo!)) {
      Toast.toast("证件号码只包含由字母和数字");
      return false;
    }
    if(!StringUtils.isEmpty2(leads.age) && !StringUtils.isAge(leads.age.toString())) {
      Toast.toast("请输入正确的年龄值");
      return false;
    }
    if(!StringUtils.isEmpty2(leads.eMail) && !StringUtils.isEmail(leads.eMail!)) {
      Toast.toast("请输入正确的邮箱账号");
      return false;
    }
    if (StringUtils.isEmpty2(leads.saleman)) {
      Toast.toast("请选择销售顾问");
      return false;
    }
    if (StringUtils.isEmpty2(leads.level)) {
      Toast.toast("请选择意向级别");
      return false;
    }
    if (StringUtils.isEmpty2(leads.modelId)) {
      Toast.toast("请选择意向车型");
      return false;
    }
    if (StringUtils.isEmpty2(leads.clueSource)) {
      Toast.toast("请选择线索来源");
      return false;
    }
    if (leads.clueSource == 10091002 && StringUtils.isEmpty2(leads.activityId)) {
      Toast.toast("请选择市场活动");
      return false;
    }
    if (!StringUtils.isEmpty2(leads.contactPhone) && !StringUtils.isChinaPhoneLegal(leads.contactPhone!)) {
      Toast.toast("请输入正确的联系人手机号");
      return false;
    }
    if(leads.clueSource == 10091013 && (leads.filesList == null || leads.filesList!.isEmpty)) {
      Toast.toast("请上传附件");
      return false;
    }
    return true;
  }
}

String? fullDateFromJson(String? dateTime) {
  return (dateTime == null || dateTime == "") ? ""  : DateUtil.formatDate(DateTime.parse(dateTime), format: DateFormats.full);
}

String? ymdDateFromJson(String? dateTime) {
  return (dateTime == null || dateTime == "") ? "" : DateUtil.formatDate(DateTime.parse(dateTime), format: DateFormats.y_mo_d);
}

String? strToJson(dynamic str) {
  return str == null ? "" : str.toString();
}

String? stringFromInt(int? value) {
  return value == null ? "" : value.toString();
}




